package com.xiang;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PicXuanZhuan {

  public static void main(String[] args) {
    String inputImagePath = "C:\\Users\\zeng_\\Pictures\\load(1)(1)\\旋转照片_test_rotated.jpg";
    String outputImagePath = "C:\\Users\\zeng_\\Pictures\\load(1)(1)\\旋转照片_test_rotated444.jpg";

    try {
      File file = new File(inputImagePath);
      // 读取输入图片
      BufferedImage inputImage = ImageIO.read(file);

//      int rotateAngle = getRotateAngle(new FileInputStream(file));
      // 旋转图片
      BufferedImage rotatedImage = rotateImage(inputImage, 180); // 旋转角度为90度

      // 保存旋转后的图片
      ImageIO.write(rotatedImage, "jpg", new File(outputImagePath));

      System.out.println("图片旋转完成。");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  //通用图片旋转
  public static BufferedImage rotateImage(BufferedImage image, double angle) {
    double radian = Math.toRadians(angle);

    // 计算旋转后的图片尺寸
    double sin = Math.abs(Math.sin(radian));
    double cos = Math.abs(Math.cos(radian));
    int newWidth = (int) Math.round(image.getWidth() * cos + image.getHeight() * sin);
    int newHeight = (int) Math.round(image.getWidth() * sin + image.getHeight() * cos);

    // 创建旋转后的图片
    BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = rotatedImage.createGraphics();

    // 执行旋转操作
    AffineTransform transform = new AffineTransform();
    transform.translate((newWidth - image.getWidth()) / 2, (newHeight - image.getHeight()) / 2);
    transform.rotate(radian, image.getWidth() / 2, image.getHeight() / 2);
    g2d.setTransform(transform);
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();

    return rotatedImage;
  }


  // 定义一个方法，根据图片文件获取图片的旋转角度
  public static int getRotateAngle(InputStream inputStream) {
    int angle = 0;
    try {
      // 读取图片的元数据
      Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
      // 获取exif信息
      ExifIFD0Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
      if (directory != null && directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
        // 根据exif信息中的方向标签，判断图片的旋转角度
        int orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
        if (orientation == 1) {
          angle = 0;
          log.info("1 无需旋转");
        } else if (orientation == 6) {
          angle = 90;
          log.info("6 旋转90");
        } else if (orientation == 3) {
          angle = 180;
          log.info("3 旋转180");
        } else if (orientation == 8) {
          angle = 270;
          log.info("8 旋转270");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    log.info("返回angle: {}", angle);
    return angle;
  }

}
